package plugins.test;

import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import org.json.simple.JSONArray;

import java.util.HashMap;
import java.util.Map;

public class GetData implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String, String> result = new HashMap<>();
        if ("GET_Data".equalsIgnoreCase(action)) {
            // 获取用户输入
            String currentName = params.get("currentName");

            // 1. 获取用户数据
            DynamicObject[] user = BusinessDataServiceHelper.load("dcb4_userdata",
                    "name, dcb4_sex, dcb4_jiguan, dcb4_height, dcb4_weight, dcb4_warn, dcb4_bmi, dcb4_healthylevel",
                    new QFilter("name", QCP.equals, currentName).toArray());

            // 2. 获取订单历史
            DynamicObject[] orderHistory = BusinessDataServiceHelper.load("dcb4_order_history",
                    "dcb4_user, dcb4_merchant, dcb4_dishes",
                    new QFilter("dcb4_buyer", QCP.equals, currentName).toArray());

            // 3. 获取菜品数据（重点修改部分）
            // 只查询主实体字段，营养信息在单据体中
            DynamicObject[] dishes = BusinessDataServiceHelper.load("dcb4_dishes",
                    "id, dcb4_merchant, name, dcb4_nutritionfactslist",
                    new QFilter("name", QCP.not_equals, "").toArray());

            // 4. 单独获取营养信息（从单据体）
            Map<Long, JSONObject> nutritionMap = new HashMap<>();
            for (DynamicObject dish : dishes) {
                DynamicObjectCollection nutritionList = dish.getDynamicObjectCollection("dcb4_nutritionfactslist");
                for (DynamicObject nutrition : nutritionList) {
                    JSONObject nutritionObj = new JSONObject();

                    // nutritionObj.put("material", nutrition.getString("dcb4_material"));
                    nutritionObj.put("protein", nutrition.getString("dcb4_protein"));
                    nutritionObj.put("carbs", nutrition.getString("dcb4_carbs"));
                    nutritionObj.put("fat", nutrition.getString("dcb4_fat"));
                    nutritionObj.put("vitamin", nutrition.getString("dcb4_vitamin"));
                    nutritionObj.put("na", nutrition.getString("dcb4_na"));
                    nutritionObj.put("energy", nutrition.getString("dcb4_energy"));
                    nutritionMap.put(dish.getLong("id"), nutritionObj);
                }
            }

            // 创建结果集
            JSONArray jsonArray = new JSONArray();

            // 5. 添加用户数据
            for (DynamicObject userObj : user) {
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("type", "user");
                jsonObj.put("name", userObj.getString("name"));
                jsonObj.put("jiguan", userObj.getString("dbc4_jiguan"));
                jsonObj.put("height", userObj.getString("dbc4_height"));
                jsonObj.put("weight", userObj.getString("dbc4_weight"));
                jsonObj.put("warn", userObj.getString("dbc4_warn"));
                jsonObj.put("bmi", userObj.getString("dbc4_bmi"));
                jsonObj.put("healthylevel", userObj.getString("dbc4_healthylevel"));
                jsonArray.add(jsonObj);
            }

            // 6. 添加订单数据
            for (DynamicObject order : orderHistory) {
                JSONObject orderObj = new JSONObject();
                orderObj.put("type", "order");
                orderObj.put("user", order.getString("dcb4_user"));
                orderObj.put("merchant", order.getString("dcb4_merchant"));
                orderObj.put("dishes", order.getString("dcb4_dishes"));
                jsonArray.add(orderObj);
            }

            // 7. 添加菜品数据（包含营养信息）
            for (DynamicObject dish : dishes) {
                JSONObject dishObj = new JSONObject();
                dishObj.put("type", "dish");
                dishObj.put("id", dish.getLong("id"));
                dishObj.put("merchant", dish.getString("dcb4_merchant"));
                dishObj.put("name", dish.getString("name"));

                // 添加关联的营养信息
                JSONObject nutrition = nutritionMap.get(dish.getLong("id"));
                if (nutrition != null) {
                    dishObj.putAll(nutrition);
                }

                jsonArray.add(dishObj);
            }

            // 8. 返回结果
            System.out.println(jsonArray.toJSONString());
            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
        return result;
    }
}
